import 'dart:math';

import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/cache/fm_cache_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_jump_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_language_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_route_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_screen_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_progress_hud.dart';
import 'package:furaha_mkopo/commonUI/fm_toast.dart';
import 'package:furaha_mkopo/manager/fm_global_manager.dart';
import 'package:furaha_mkopo/page/authentication/fm_identity_auth_page.dart';
import 'package:furaha_mkopo/page/base/fm_base_page_widget.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_click_access_api.dart';
import 'package:furaha_mkopo/page/home/api/fm_home_detail_api.dart';
import 'package:furaha_mkopo/page/home/controller/fm_home_big_controller.dart';
import 'package:furaha_mkopo/page/home/dialog/fm_auth_completed_dialog.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_click_access_entity.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_detail_entity.dart';
import 'package:furaha_mkopo/page/home/entity/fm_home_entity.dart';
import 'package:furaha_mkopo/page/home/orderDetail/emergency/fm_emergency_page.dart';
import 'package:furaha_mkopo/page/home/orderDetail/job/fm_job_page.dart';
import 'package:furaha_mkopo/page/home/orderDetail/personal/fm_personal_page.dart';
import 'package:furaha_mkopo/page/login/fm_login_page.dart';
import 'package:geolocator_platform_interface/src/models/position.dart';
import 'package:get/get.dart';

import '../authentication/dialog/fm_cupertino_image_picker_sheet.dart';


///大卡位
class FMHomeLargeCardSlotPage extends FMBasePageWidget {
  FMHomeBeamedEntity beamedEntity;

  FMHomeLargeCardSlotPage({super.key, required this.beamedEntity});

  @override
  State<FMHomeLargeCardSlotPage> createState() => _FMHomeLargeCardSlotPageState();
}

class _FMHomeLargeCardSlotPageState extends FMBasePageState<FMHomeLargeCardSlotPage> {
  final controller = Get.put(FMHomeBigController());

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: (){
        String sessionId = FMCacheTool.getSessionId();
        if (sessionId.isEmpty) {
          FMJumpTool.jumpLogin(context: context);
        }
      },
      child: Stack(
        children: [
          Positioned(
              left: 0,
              right: 0,
              bottom: 0,
              child: Container(
                height: 198,
                width: double.infinity,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(FMLanguageTool.getAssets('assets/home/ic_big_bottom_tip1',suffix: ".webp")),
                  ),
                ),
              )),
          Positioned(
            top: 90 + offsetTop(context),
            left: 0,
            right: 0,
            child: Container(
              height: 398,
              width: double.infinity,
              decoration: const BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('assets/home/ic_bg_card_slot_bg.png'),
                    fit: BoxFit.cover
                ),
              ),
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  controller.onRequestClickAccess(
                      widget.beamedEntity.voice?.first.useless.toString() ?? "",
                      onTapAuthentication);
                },
                child: Column(
                  children: [
                    const SizedBox(
                      height: 16,
                    ),
                    Container(
                        width: 210,
                        alignment: Alignment.centerLeft,
                        child: Image.asset(FMLanguageTool.getAssets("assets/home/ic_big_text_bg"),height: 21,)),
                    const SizedBox(
                      height: 31.43,
                    ),
                    generateTitle(),
                    const SizedBox(
                      height: 14,
                    ),
                    generateMoney(),
                    const SizedBox(
                      height: 8,
                    ),
                    generateLeftRightCard(),
                    const SizedBox(
                      height: 8,
                    ),
                    generateDescribe(),
                    const SizedBox(
                      height: 10,
                    ),
                    generateStepImage(),
                  ],
                ),
              ),
            ),
          ),
          Positioned(
              top: 423 + offsetTop(context),
              left: 0,
              right: 0,
              child: SizedBox(
                height: 151,
                width: double.infinity,
                child: Stack(
                  children: [
                    Positioned(
                        top: 0,
                        left: 0,
                        right: 0,
                        child: generateApplyButton()),
                    Positioned(
                        top: 1.5,
                        left: 0,
                        child: Image.asset(
                          "assets/home/ic_big_left_hand.png",
                          width: 56,
                          height: 149.5,
                        )),
                    Positioned(
                        top: 0,
                        right: 0,
                        child: Image.asset(
                          "assets/home/ic_big_right_hand.png",
                          width: 114,
                          height: 136,
                        )),
                  ],
                ),
              )),
        ],
      ),
    );
  }

  Widget generateTitle() {
    String title;
    if (widget.beamedEntity.voice == null ||
        widget.beamedEntity.voice?.isEmpty == true) {
      title = "--";
    } else {
      title = widget.beamedEntity.voice?.first.attempt ?? "--";
    }
    return Text(
      title,
      style: const TextStyle(fontSize: 17, color: Colors.black),
    );
  }

  Widget generateMoney() {
    String money;
    if (widget.beamedEntity.voice == null ||
        widget.beamedEntity.voice?.isEmpty == true) {
      money = "--";
    } else {
      money = widget.beamedEntity.voice?.first.wheel ?? "--";
    }
    const angle = (180-131) * (pi / 180);
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 27),
      child: ShaderMask(
        shaderCallback: (bounds) => LinearGradient(
          colors: const [Color(0xFFFF8400), Color(0xFFFFE100)],
          begin: Alignment(-cos(angle), sin(angle)), // 根据三角函数计算
          end: Alignment(cos(angle), -sin(angle)),
        ).createShader(bounds),
        blendMode: BlendMode.srcIn,
        child: AutoSizeText(
          money,
          maxLines: 1,
          minFontSize: 5,
          maxFontSize: 60,
          style: const TextStyle(
            fontSize: 60,
            fontWeight: FontWeight.bold, // 可保留其他样式
          ),
        ),
      ),
    );
  }

  Widget generateLeftRightCard() {
    String doors = "--";
    String throne = "--";
    String steps = "--";
    String rarely = "--";
    if (widget.beamedEntity.voice != null ||
        widget.beamedEntity.voice?.isNotEmpty == true) {
      doors = widget.beamedEntity.voice?.first.doors ?? "--";
      throne = widget.beamedEntity.voice?.first.throne ?? "--";
      steps = widget.beamedEntity.voice?.first.steps ?? "--";
      rarely = widget.beamedEntity.voice?.first.rarely ?? "--";
    }
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 27),
      height: 72,
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/home/ic_big_card_bg.png'),
          fit: BoxFit.cover,
        ),
      ),
      child: Row(
        children: [
          Expanded(
              child: Row(
            children: [
              const SizedBox(
                width: 12,
              ),
              Image.asset(
                "assets/home/ic_big_left_bg.png",
                width: 41,
                height: 41,
              ),
              const SizedBox(
                width: 8,
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    doors,
                    style: const TextStyle(
                        fontSize: 15,
                        color: Colors.black,
                        fontWeight: FontWeight.bold),
                  ),
                  Text(
                    throne,
                    style: const TextStyle(fontSize: 12, color: Colors.black),
                  ),
                ],
              )
            ],
          )),
          Expanded(
              child: Row(
            children: [
              const SizedBox(
                width: 12,
              ),
              Image.asset(
                "assets/home/ic_big_right_bg.png",
                width: 41,
                height: 41,
              ),
              const SizedBox(
                width: 8,
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    steps,
                    style: const TextStyle(
                        fontSize: 15,
                        color: Colors.black,
                        fontWeight: FontWeight.bold),
                  ),
                  Text(
                    rarely,
                    style: const TextStyle(fontSize: 12, color: Colors.black),
                  ),
                ],
              )
            ],
          )),
        ],
      ),
    );
  }

  Widget generateDescribe() {
    String describe = "--";
    if (widget.beamedEntity.voice != null ||
        widget.beamedEntity.voice?.isNotEmpty == true) {
      describe = widget.beamedEntity.voice?.first.requested ?? "--";
    }
    return Text(
      describe,
      style: const TextStyle(
          color: Color(0xFF158F05),
          fontSize: 14,
          fontWeight: FontWeight.w600,
          decoration: TextDecoration.underline,
          decorationThickness: 2),
    );
  }

  Widget generateStepImage(){
    return Container(
        margin: const EdgeInsets.symmetric(horizontal: 27),
        child: Image.asset(FMLanguageTool.getAssets("assets/home/ic_big_step_bg")));
  }

  Widget generateApplyButton() {
    String passing = "--";
    if (widget.beamedEntity.voice != null ||
        widget.beamedEntity.voice?.isNotEmpty == true) {
      passing = widget.beamedEntity.voice?.first.passing ?? "--";
    }
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: (){
        controller.onRequestClickAccess(
            widget.beamedEntity.voice?.first.useless.toString() ?? "",
            onTapAuthentication);
      },
      child: Container(
        height: 82,
        width: double.infinity,
        alignment: Alignment.center,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/home/ic_big_btn_bg.png'),
            fit: BoxFit.fill,
          ),
        ),
        child: Container(
          padding: const EdgeInsets.only(top: 8),
          child: Text(
            passing,
            style: const TextStyle(fontSize: 20, color: Colors.white, fontWeight: FontWeight.bold),
          ),
        ),
      ),
    );
  }

  void onTapAuthentication(String tag,String? bidding,String curListen){
    if(tag.isEmpty){
      controller.onRequestOught(authCompleted);
      return;
    }
    FMJumpTool.jumpAuth(context, tag, bidding, curListen, false);
  }

  void authCompleted(String bidding){
    FMJumpTool.jumpWidget(url: bidding);
    // showDialog(
    //     context: context,
    //     builder: (context) {
    //       return FMAuthCompletedDialog(
    //         onConfirm: () {
    //           Navigator.of(context).pop();
    //           FMJumpTool.jumpWebWidget(context: this.context, url: bidding);
    //         },
    //       );
    //     });
  }

  double offsetTop(BuildContext context){
    return MediaQuery.of(context).viewPadding.top;
  }

  @override
  void onLocationError(String error) {
    // TODO: implement onLocationError
  }

  @override
  void onLocationUpdated(Position position) {
    controller.position = position;
  }
}
